//
//  CustomStackView.swift
//  TestUIKit
//
//  Created by 100Years on 2025/2/24.
//

import UIKit
import SnapKit

open class TSCustomStackView: UIView {
    // 内部的 UIScrollView 和 UIStackView
    public let scrollView: UIScrollView
    public let stackView: UIStackView
    
    // 开放的属性，用于设置方向和间距
    public var axis: NSLayoutConstraint.Axis {
        get {
            return stackView.axis
        }
        set {
            stackView.axis = newValue
            updateScrollViewConstraints()
        }
    }
    
    public var spacing: CGFloat {
        get {
            return stackView.spacing
        }
        set {
            stackView.spacing = newValue
        }
    }
    
    public var viewH:CGFloat {
        get {
            return scrollView.contentSize.height
        }
    }
    
    // 初始化方法
    public init(axis: NSLayoutConstraint.Axis = .vertical,alignment:UIStackView.Alignment = .leading, spacing: CGFloat = 0) {
        self.scrollView = UIScrollView()
        self.scrollView.showsVerticalScrollIndicator = false
        self.scrollView.showsHorizontalScrollIndicator = false
        self.stackView = UIStackView()
        self.stackView.axis = axis
        self.stackView.spacing = spacing
        self.stackView.alignment = alignment
        self.stackView.distribution = .fill
        super.init(frame: .zero)
        setupUI()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置 UI
    private func setupUI() {
        // 添加 scrollView
        addSubview(scrollView)
        scrollView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        // 添加 stackView 到 scrollView
        scrollView.addSubview(stackView)
        updateScrollViewConstraints()
    }
    
    // 根据轴方向更新约束
    private func updateScrollViewConstraints() {
        stackView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
            
            // 根据轴方向设置 contentSize
            if axis == .vertical {
                make.width.equalTo(scrollView)
            } else {
                make.height.equalTo(scrollView)
            }
        }
    }
    
    // 动态添加子视图的方法
    public func addArrangedSubview(_ view: UIView) {
        stackView.addArrangedSubview(view)
    }
    
    // 动态添加子视图的方法
    public func addSubviewToStack(_ view: UIView,length:CGFloat? = nil) {
        stackView.addArrangedSubview(view)
        view.snp.makeConstraints { make in
            if axis == .vertical {
                make.width.equalTo(stackView)
                if let length = length {
                    make.height.equalTo(length)
                }
            } else {
                make.height.equalTo(stackView)
                if let length = length {
                    make.width.equalTo(length)
                }
            }
        }
    }
    
    //动态添加子视图的方法(添加到白板一个空板 View)
    public func addSubviewToStackWhiteBoard(_ view: UIView,length:CGFloat? = nil) {
        let bgView = UIView()
        bgView.addSubview(view)
        addSubviewToStack(bgView,length: length)
    }
    
    // 在指定位置插入子视图
    public func insertViewToStack(_ view: UIView, at stackIndex: Int) {
        stackView.insertArrangedSubview(view, at: stackIndex)
        // 可以根据需要对子视图进行额外的布局设置
        view.snp.makeConstraints { make in
            if axis == .vertical {
                make.width.equalTo(stackView)
            } else {
                make.height.equalTo(stackView)
            }
        }
    }
    
    // 移除子视图
    public func removeViewToStack(_ view: UIView) {
        stackView.removeArrangedSubview(view)
        view.removeFromSuperview()
    }
    
    public func addSpacing(length:CGFloat) {
        let view = UIView()
        addSubviewToStack(view)
        view.snp.makeConstraints { make in
            if axis == .vertical {
                make.height.equalTo(length)
            } else {
                make.width.equalTo(length)
            }
        }
    }
}

extension UIStackView {
    public func addSpacing(length:CGFloat) {
        let view = UIView()
        self.addArrangedSubview(view)
        view.snp.makeConstraints { make in
            if axis == .vertical {
                make.width.equalToSuperview()
                make.height.equalTo(length)
            } else {
                make.width.equalTo(length)
                make.height.equalToSuperview()
            }
        }
    }
    
    public func removeAllSubview(length:CGFloat) {
        let view = UIView()
        self.addArrangedSubview(view)
        view.snp.makeConstraints { make in
            if axis == .vertical {
                make.width.equalToSuperview()
                make.height.equalTo(length)
            } else {
                make.width.equalTo(length)
                make.height.equalToSuperview()
            }
        }
    }
    
    public func removeAllArrangedSubviews() {
        arrangedSubviews.forEach {
            removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
    }
    
    public static func createStackView(
        axis:NSLayoutConstraint.Axis = .vertical,
        spacing: CGFloat = 0,
        distribution:UIStackView.Distribution = .fill,
        alignment:UIStackView.Alignment = .fill
    ) -> UIStackView{
        let stackView = UIStackView()
        stackView.axis = axis
        stackView.spacing = spacing
        stackView.alignment = alignment
        return stackView
    }

}
